package org.gis.utils.activator;

import java.lang.reflect.Constructor;

/**
 *
 * @author gus
 */
public class Activator
{
    public static Object instance( String fullName) throws Exception
    {
        return instance(fullName, new Object[0]);
    }
    
    
    public static Object instance( String fullName, Object[] ctorParameters ) throws Exception
    {
        if( fullName == null || fullName.isEmpty())
        {
            throw new IllegalArgumentException(String.format("argument = %s  value = %s", "fullname", fullName)); 
        }
        
        Class clazz = Class.forName(fullName);
        return instance(clazz, ctorParameters);
    }

    public static Object instance( Class clazz, Object [] ctorParameters ) throws Exception
    {
        if( clazz == null )
        {
            throw new IllegalArgumentException("clazz");
        }
        Constructor ctor = clazz.getConstructor(getTypes(ctorParameters));
        return ctor.newInstance(ctorParameters);
    }
    
    public static Object instance( Class clazz) throws Exception
    {
        return instance(clazz, new Object[0]);
    }
    
    private static Class[] getTypes( Object[] ctorParametes )
    {
        Class[] types = new Class[ctorParametes.length];

        for ( int i = 0; i < ctorParametes.length; i++ )
        {
            types[i] = ctorParametes[i].getClass();
        }
        return types;
    }
}
